package com.heima.article.service.Impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojo.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class HotArticleServiceImpl implements HotArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Override
    public void computeHotArticle() {
        Date date = DateTime.now().minusDays(5).toDate();

        List<ApArticle> articleListByLast5Day = apArticleMapper.findArticleListByLast5Day(date);

        List<HotArticleVo> hotArticleVos = computeHotArticle(articleListByLast5Day);

        cacheTagToRedis(hotArticleVos);



    }

    @Autowired
    private IWemediaClient wemediaClient;

    @Autowired
    private CacheService cacheService;

    private void cacheTagToRedis(List<HotArticleVo> hotArticleVos) {

        ResponseResult channels = wemediaClient.getChannels();

        if (channels.getCode().equals(200)){
            String s = JSON.toJSONString(channels.getData());
            List<WmChannel> wmChannels = JSON.parseArray(s, WmChannel.class);
            for (WmChannel wmChannel : wmChannels) {
                List<HotArticleVo> collect = hotArticleVos.stream().filter(x -> x.getChannelId().equals(wmChannel.getId())).collect(Collectors.toList());
                //为每个频道设置热门article;
                collect = collect.stream().sorted(Comparator.comparing(HotArticleVo::getScore)).collect(Collectors.toList());
                if (collect!=null&&collect.size()>30){
                    collect = collect.subList(0, 30);
                }
                cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE+wmChannel.getId(),JSON.toJSONString(collect));
            }
        }

        List<HotArticleVo> collect2 = hotArticleVos.stream().sorted(Comparator.comparing(HotArticleVo::getScore)).collect(Collectors.toList());
        if (collect2!=null&&collect2.size()>30){
            collect2 = collect2.subList(0, 30);
        }
        cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE+ArticleConstants.DEFAULT_TAG,JSON.toJSONString(collect2));
    }

    private List<HotArticleVo> computeHotArticle(List<ApArticle> articleListByLast5Day) {

        List<HotArticleVo> hotArticleVos = new ArrayList<>();
        if (articleListByLast5Day!=null&&articleListByLast5Day.size()>0){
            for (ApArticle apArticle : articleListByLast5Day) {
                HotArticleVo hotArticleVo = new HotArticleVo();
                BeanUtils.copyProperties(apArticle,hotArticleVo);
                Integer integer = computeScore(apArticle);
                hotArticleVo.setScore(integer);
                hotArticleVos.add(hotArticleVo);
            }
        }

        return hotArticleVos;
    }

    private Integer computeScore(ApArticle apArticle) {
        Integer score = 0;
        if (apArticle!=null){

            if (apArticle.getLikes()!=null){
                score += apArticle.getLikes()* ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;
            }
            if (apArticle.getViews()!=null){
                score += apArticle.getViews();
            }
            if (apArticle.getComment()!=null){
                score += apArticle.getComment()*ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;
            }
            if (apArticle.getCollection()!=null){
                score += apArticle.getCollection()*ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;
            }
        }
        return score;

    }
}
